#ifndef OMG_DDS_SUB_DATA_READER_HPP_
#define OMG_DDS_SUB_DATA_READER_HPP_

/* Copyright (c) 2009-2010, Real-Time Innovations, Inc.
 * Copyright (c) 2010, Object Management Group, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the names of the above copyright holders nor the names of their
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL A COPYRIGHT HOLDER OR CONTRIBUTOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


namespace dds { namespace sub {

template <typename T>
class DataReader : public DataReaderBase {
    OMG_DDS_REFERENCE_TYPE(DataReader)

public:
    typedef T                                       DataType;
    typedef Sample<DataType>                        SampleType;
    typedef LoanedSamples<DataType>                 LoanedSamplesType;
    typedef dds::core::StatusCondition<DataReader>  StatusConditionType;
    typedef ReadCondition<DataType>                 ReadConditionType;
    typedef QueryCondition<DataType>                QueryConditionType;

public:
    LoanedSamplesType read(
        int                             max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::status::SampleState&   sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&     view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState& instance_states = dds::core::status::InstanceState::any());

    LoanedSamplesType take(
        int                             max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::status::SampleState&   sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&     view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState& instance_states = dds::core::status::InstanceState::any());

    LoanedSamplesType read(
        const ReadConditionType& a_condition,
        int                      max_samples = dds::core::LENGTH_UNLIMITED);

    LoanedSamplesType take(
        const ReadConditionType& a_condition,
        int                      max_samples = dds::core::LENGTH_UNLIMITED);

    /**
     * Return true if data was read or false if none was available.
     */
    bool read_next(SampleType& sample);
    
    /**
     * Return true if data was taken or false if none was available.
     */
    bool take_next(SampleType& sample);

    LoanedSamplesType read_instance(
        const dds::core::InstanceHandle& a_handle,
        int                             max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::status::SampleState&   sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&     view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState& instance_states = dds::core::status::InstanceState::any());

    LoanedSamplesType take_instance(
        const dds::core::InstanceHandle& a_handle,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::status::SampleState&    sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&      view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState&  instance_states = dds::core::status::InstanceState::any());

    LoanedSamplesType read_next(
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::status::SampleState&    sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&      view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState&  instance_states = dds::core::status::InstanceState::any());

    LoanedSamplesType take_next(
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::status::SampleState&    sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&      view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState&  instance_states = dds::core::status::InstanceState::any());

    LoanedSamplesType read_next(
        const ReadConditionType&         a_condition,
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED);

    LoanedSamplesType take_next(
        const ReadConditionType&         a_condition,
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED);

public:
    dds::topic::TopicDescription<DataType> topic_description();
    
public:
    ReadConditionType create_readcondition(
        const dds::core::status::SampleState&   sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&     view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState& instance_states = dds::core::status::InstanceState::any());
    
#ifdef OMG_DDS_CONTENT_SUBSCRIPTION_SUPPORT
    QueryConditionType create_querycondition(
        const std::string& query_expression,
        const dds::core::StringSeq& query_parameters,
        const dds::core::status::SampleState&   sample_states   = dds::core::status::SampleState::any(),
        const dds::core::status::ViewState&     view_states     = dds::core::status::ViewState::any(),
        const dds::core::status::InstanceState& instance_states = dds::core::status::InstanceState::any());
#endif  // OMG_DDS_CONTENT_SUBSCRIPTION_SUPPORT

public:
    DataType& key_value(DataType& key_holder, 
                        const dds::core::InstanceHandle& handle) const;
    DataType key_value(const dds::core::InstanceHandle& handle) const;

    dds::core::InstanceHandle lookup_instance(const DataType& key_holder);

    // --- Entity methods: --- //
public:
    StatusConditionType status_condition();

    void listener(DataReaderListener<DataType>* l,
                  const dds::core::status::StatusMask& mask = dds::core::status::StatusMask::all());

    DataReaderListener<DataType>* listener() const;
};

} }

#endif /* OMG_DDS_SUB_DATA_READER_HPP_ */
